<?php

namespace app\api\validate;

use app\common\business\Category;
use app\common\business\WorkType;
use app\common\lib\Str;
use think\Validate;
use think\Exception;

class UserInfo extends Validate
{
    protected $rule = [
        'education' => 'require|number|checkEducation',
        'experience' => 'require|number|checkExperience',
        'intention' => 'require|number|checkIntention',
        'free' => 'require|number|checkFree',
        'introduce' => 'require',
        'skill_certificate' => 'require',
        'skill_label' => 'require|checkLabel',
    ];

    protected $message = [
        'education.require' => '学历必须',
        'education.number' => '学历错误',
        'experience.require' => '经验必须',
        'experience.number' => '经验错误',
        'intention.require' => '求职意向必须',
        'intention.number' => '求职意向错误',
        'free.require' => '空闲时间必须',
        'free.number' => '空闲时间错误',
        'introduce' => '个人简介必须',
        'skill_certificate' => '技能证书必须',
        'skill_label' => '技能标签必须',
    ];

    protected function checkEducation($value, $rule, $data = [])
    {
        return $this->checkCommonCategory('学历',$value);
    }
    protected function checkExperience($value, $rule, $data = [])
    {
        return $this->checkCommonCategory('经验',$value);
    }
    protected function checkFree($value, $rule, $data = [])
    {
        return $this->checkCommonCategory('空闲时间',$value);
    }
    protected function checkCommonCategory($name,$value)
    {
        try {
            (new Category())->checkValidValueByName($name,$value);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }
    protected function checkIntention($value, $rule, $data = [])
    {
        try {
            (new WorkType())->checkValidValue($value);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }
    protected function checkLabel($value, $rule, $data = [])
    {
        try {
            (new WorkType())->checkValidValueMultiple($value);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }

    protected $scene = [
        'update' => ['education','experience','intention','free'],
        'update_label' => ['introduce','skill_label'],
    ];
}